#include <string>

#include "TcpServer.h"
#include "Logger.h"

class EchoServer
{
public:
    EchoServer(EventLoop *loop, const InetAddress &addr, const std::string &name)
        : server_(loop, addr, name)
        , loop_(loop)
    {
        // 注册回调函数
        server_.setConnectionCallback(
            std::bind(&EchoServer::onConnection, this, std::placeholders::_1));
        
        server_.setMessageCallback(
            std::bind(&EchoServer::onMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        // 设置合适的subloop线程数量
        server_.setThreadNum(3);
    }
    void start()
    {
        server_.start();
    }

private:
    // 连接建立或断开的回调函数
    void onConnection(const TcpConnectionPtr &conn)   
    {
        if (conn->connected())
        {
            LOG_INFO("Connection UP : %s", conn->peerAddress().toIpPort().c_str());
        }
        else
        {
            LOG_INFO("Connection DOWN : %s", conn->peerAddress().toIpPort().c_str());
        }
    }

    // 可读写事件回调
    void onMessage(const TcpConnectionPtr &conn, Buffer *buf, Timestamp time)
    {
        std::string msg = buf->retrieveAllAsString();
        conn->send(msg);
        // conn->shutdown();   // 关闭写端 底层响应EPOLLHUP => 执行closeCallback_
    }
    TcpServer server_;
    EventLoop *loop_;

};

int main() {
    EventLoop loop;
    InetAddress addr(8080,"0.0.0.0");
    EchoServer server(&loop, addr, "EchoServer");
    server.start();
    loop.loop();
    return 0;
}




// #include <string>
// #include "TcpServer.h"
// #include "Logger.h"

// class EchoServer{
// public:
//   EchoServer(EventLoop* loop,InetAddress& addr,const std::string& s)
//       :server(loop,addr,s)
//       ,loop(loop)
//       {}

//   void start(){
//     server.setConnectionCallback(std::bind(&EchoServer::onConnection,this,std::placeholders::_1));

//     server.setMessageCallback(std::bind(&EchoServer::onMessage,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3));

//     server.setThreadNum(3);
//   }
// private:
//   void onConnection(const TcpConnectionPtr& conn){
//     if(conn->connected()){
//       LOG_INFO("establish a connection : %s",conn->peerAddress().toIpPort().c_str());
//       LOG_INFO("Connection UP : %s", conn->peerAddress().toIpPort().c_str());
//     }
//     else{
//       LOG_INFO("close a connection:%s",conn->peerAddress().toIpPort().c_str());
//     }
//   }
//   void onMessage(const TcpConnectionPtr& conn,Buffer* buffer,Timestamp time){
//     std::string msg=buffer->retrieveAllAsString();
//     LOG_INFO("read a message:%s",msg.c_str());
//   }
  
// private:
//   TcpServer server;
//   EventLoop* loop;
// };


// int main(){
//     InetAddress addr(18080);
//     EventLoop loop;
//     EchoServer server(&loop,addr,"EchoServer");
//     server.start();
//     loop.loop();
//   }